import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { assetWorkAreaStateType } from './typings'
import {
  getWorkAreaSelectListByUser,
  addWorkArea,
  updateWorkArea,
  delateWorkArea,
  getXzqData,
  updateSortWorkArea
  // getLamppostClassify
} from '~/api/assetManagement/assetAreaManage/assetWorkArea'
import { getTenantDictDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

const assetWorkArea: Module<assetWorkAreaStateType, assetWorkAreaStateType> = {
  namespaced: true,
  state: {
    workAreaSelectList: [],
    workAreaEnum: {},
    polygonData: {},
    color: {},
    postHeightTypeOpt: []
  },
  getters: {
    workAreaSelectList: (state) => state.workAreaSelectList,
    workAreaEnum: (state) => state.workAreaEnum,
    polygonData: (state) => state.polygonData, // 编辑的面数据
    color: (state) => state.color, // 编辑的面颜色
    postHeightTypeOpt: (state) => state.postHeightTypeOpt
  },
  mutations: {
    SET_WORKAREA_SELECT_LIST: (state, payload) => {
      state.workAreaSelectList = payload
      state.workAreaEnum = filterValueEnum(payload, 'id', 'name')
    },
    SET_POLYGON_INFO: (state, payload) => {
      state.polygonData = payload
    },
    SET_COLOR_INFO: (state, payload) => {
      state.color = payload
    },
    GET_POSTHEIGHTTYPE: (state, payload) => {
      state.postHeightTypeOpt = payload
    }
  },
  actions: {
    // 字典-杆高分类
    async GetPostHeightType<S, T>({ commit }: ActionContext<S, T>) {
      return await getTenantDictDataDictionaryByCode({ code: 'post_height_type' }).then(
        (res: AxiosResponse) => {
          commit('GET_POSTHEIGHTTYPE', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 获取所属区域下拉列表
    async GetWorkAreaSelectListByUser<S, T>({ commit }: ActionContext<S, T>) {
      return await getWorkAreaSelectListByUser().then((res: AxiosResponse) => {
        commit('SET_WORKAREA_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 查询工区列表
    GetAreaList() {
      return new Promise((resolve, reject) => {
        getWorkAreaSelectListByUser()
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 添加工区
    AddWorkArea<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        addWorkArea<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 修改工区
    UpdateWorkArea<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateWorkArea<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 删除工区接口
    DelateWorkArea<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        delateWorkArea<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 行政区接口
    GetXzqData() {
      return new Promise((resolve, reject) => {
        getXzqData()
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 工区动态排序接口
    UpdateSortWorkArea<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateSortWorkArea(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default assetWorkArea
